package com.app.piemoment.api;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;

import com.app.piemoment.config.Config;
import com.app.piemoment.entity.LoginInfo;
import com.app.piemoment.net.HttpResult;
import com.app.piemoment.net.HttpResultJson;
import com.app.piemoment.net.NetService;

public class MatchApi extends BaseApi {

	protected static final String TAG = "MatchApi";

	public MatchApi(Context context) {
		super(context);
	}

	/**
	 * 获取全部有效配对
	 * @param requestCode
	 * @param context
	 */
	public void getMatchs(final int requestCode,final Context context) {
		
		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				
				try {
					params.add(new BasicNameValuePair("format","json"));
					params.add(new BasicNameValuePair("token",URLEncoder.encode(LoginInfo.getInstance().getAccess_token(),"UTF-8")));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				
				HttpResultJson result = NetService.httpGetReturnJson(context,Config.HOME_URL + Config.GET_MATCH, params);
				
				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject jsonObject = result.getJson();
					apiResult.setJsonEntity(jsonObject);
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}
			
			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
			}
			
			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}
			
		}.execute();
	}

	/**
	 * 获取全部有效配对对象信息
	 * @param requestCode
	 * @param context
	 */
	public void getMatchDetails(final int requestCode,final Context context) {

		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				List<NameValuePair> params = new ArrayList<NameValuePair>();

				try {
					params.add(new BasicNameValuePair("format","json"));
					params.add(new BasicNameValuePair("token",URLEncoder.encode(LoginInfo.getInstance().getAccess_token(),"UTF-8")));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				
				HttpResultJson result = NetService.httpGetReturnJson(context,Config.HOME_URL + Config.GET_MATCH_DETAIL, params);

				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject jsonObject = result.getJson();
					apiResult.setJsonEntity(jsonObject);
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}

			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
			}

			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}

		}.execute();
	}
	/**
	 * 获取mid 配对信息
	 * @param requestCode
	 * @param context
	 */
	public void getMatchById(final int requestCode,final Context context,final String mid) {
		
		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				
				try {
					params.add(new BasicNameValuePair("format","json"));
					params.add(new BasicNameValuePair("token",URLEncoder.encode(LoginInfo.getInstance().getAccess_token(),"UTF-8")));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				
				HttpResultJson result = NetService.httpGetReturnJson(context,Config.HOME_URL + String.format(Config.GET_MID_MATCH, mid), params);
				
				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject jsonObject = result.getJson();
					apiResult.setJsonEntity(jsonObject);
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}
			
			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
			}
			
			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}
			
		}.execute();
	}
	/**
	 * 获取mid 配对对象信息
	 * @param requestCode
	 * @param context
	 */
	public void getMatchDetailById(final int requestCode,final Context context,final String mid) {
		
		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				
				try {
					params.add(new BasicNameValuePair("format","json"));
					params.add(new BasicNameValuePair("token",URLEncoder.encode(LoginInfo.getInstance().getAccess_token(),"UTF-8")));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				
				
				HttpResultJson result = NetService.httpGetReturnJson(context,Config.HOME_URL + String.format(Config.GET_MID_MATCH_DETAIL, mid), params);
				
				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject jsonObject = result.getJson();
					apiResult.setJsonEntity(jsonObject);
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}
			
			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
			}
			
			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}
			
		}.execute();
	}
	/**
	 * like by id
	 * @param requestCode
	 * @param context
	 */
	public void likeById(final int requestCode,final Context context,final String mid) {
		
		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				
				params.add(new BasicNameValuePair("_method","patch"));
				params.add(new BasicNameValuePair("format","json"));
				params.add(new BasicNameValuePair("token",LoginInfo.getInstance().getAccess_token()));
				
				
				HttpResultJson result = NetService.httpPostReturnJson(context,Config.HOME_URL + String.format(Config.LIKE_URL, mid), params);
				
				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject jsonObject = result.getJson();
					apiResult.setJsonEntity(jsonObject);
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}
			
			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
			}
			
			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}
			
		}.execute();
	}
	/**
	 * pass by id
	 * @param requestCode
	 * @param context
	 */
	public void passById(final int requestCode,final Context context,final String mid) {
		
		new AsyncTask<Object, Integer, ApiResult<Object>>() {
			@Override
			protected ApiResult<Object> doInBackground(Object... strs) {
				ApiResult<Object> apiResult = new ApiResult<Object>();
				apiResult.setResultCode(ApiResult.RESULT_FAIL);
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				
				params.add(new BasicNameValuePair("_method","patch"));
				params.add(new BasicNameValuePair("format","json"));
				params.add(new BasicNameValuePair("token",LoginInfo.getInstance().getAccess_token()));
				
				HttpResultJson result = NetService.httpPostReturnJson(context,Config.HOME_URL + String.format(Config.PASS_URL, mid), params);
				
				if (result.getResultCode() == HttpResult.RESULT_OK) {
					apiResult.setResultCode(ApiResult.RESULT_OK);
					JSONObject jsonObject = result.getJson();
					apiResult.setJsonEntity(jsonObject);
				} else {
					apiResult.setFailCode(result.getFailCode());
					apiResult.setFailMessage(result.getFailMessage());
				}
				return apiResult;
			}
			
			@Override
			protected void onPostExecute(ApiResult<Object> apiResult) {
				if (returnResultListener == null) {
					return;
				}
				if (apiResult.getResultCode() == ApiResult.RESULT_OK) {
					returnResultListener.onReturnSucceedResult(requestCode,
							apiResult);
				} else {
					returnResultListener.onReturnFailResult(requestCode,
							apiResult);
				}
			}
			
			@Override
			protected void onPreExecute() {
				if (returnResultListener == null) {
					return;
				}
				returnResultListener.onReturnLoading(requestCode);
				super.onPreExecute();
			}
			
		}.execute();
	}
}
